package com.fare.taluo.vo.core;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.RandomUtils;
import com.fare.taluo.vo.airfreight.StopOverEntityVO;
import com.fare.taluo.vo.airfreight.doub.CabinRelationEntityVO;

public class PlaneInfo {

	private String id = "";

	// carrierInfo
	/**
	 * 运营商简称英文，如：MU
	 */
	private String carrierKey = "";
	/**
	 * [返程]运营商简称英文，如：MU
	 */
	private String roundCarrierKey = "";

	/**
	 * 运营商简称中文，如：南航
	 */
	private String carrierName = "";
	/**
	 * [返程]运营商简称中文，如：南航
	 */
	private String roundCarrierName = "";

	private String hbaseKey;
	/**
	 * 运营商全名，如：中国海南航空公司
	 */
	private String carrierFullName = "";
	/**
	 * [返程]运营商全名，如：中国海南航空公司
	 */
	private String roundCarrierFullName = "";

	// flightInfo
	/**
	 * 航班批次，MU3540|2012-05-19
	 */
	private String flightBatch = "";

	/**
	 * 航班批次日期。2012-05-19
	 */
	private String flightBatchDate = "";
	/**
	 * [返程]航班批次日期。2012-05-19
	 */
	private String roundFlightBatchDate = "";

	/**
	 * 航班起飞时间
	 */
	private String flightBatchStartTime = "";

	/**
	 * [返程]航班起飞时间
	 */
	private String roundFlightBatchStartTime = "";

	/**
	 * 航班预计到达时间
	 */
	private String flightBatchEndTime = "";
	/**
	 * [返程]航班预计到达时间
	 */
	private String roundFlightBatchEndTime = "";

	/**
	 * 航班批次id，如： MU3540|2012-05-19
	 */
	private String flightId = "";

	/**
	 * 仓位信息
	 * */
	private String cabinInfos;

	/**
	 * 目的地
	 */
	private String toCity = "";

	private String timstamp;

	private String SOURCE_ROWKEY;

	private Integer tendency;

	/**
	 * 航班运价的类型,0为单程价,1为往返程价,2为联程
	 */
	private Integer freightType;

	/**
	 * 往返程价
	 */
	private String roundTripPrice;

	/**
	 * 联程价
	 */
	private Double interlinePrice;

	private String actuallyFlightNo;

	private List<AgentEntityVO> agents = new ArrayList<AgentEntityVO>();
	/**
	 * 仓位信息
	 */

	private List<CabinEntityVO> cabins = new ArrayList<CabinEntityVO>();
	/**
	 * 价格除了仓位外，还分.,个种类，比如燃油费，附加税费
	 */
	private List<PriceEntityVO> prices = new ArrayList<PriceEntityVO>();

	/**
	 * 中转
	 */

	private List<TransitchangeEntityVO> transit = new ArrayList<TransitchangeEntityVO>();

	/**
	 * 经停
	 */
	private List<StopOverEntityVO> stopOverlist = new ArrayList<StopOverEntityVO>();

	/**
	 * 返程，旧模型，不在使用
	 */
	private List<ReturnTripEntityVO> returnTrip = new ArrayList<ReturnTripEntityVO>();

	/**
	 * 新模型下返程航班
	 */
	private List<PlaneInfo> returnPlaneInfo = new ArrayList<PlaneInfo>();
	/**
	 * 去程的仓位信息和回程的仓位信息的对应关系 去程的经济舱对应回程的商务舱，包含了二者的打包价
	 */
	// @OneToMany
	// @HBaseColumn(qualifier = "cabinRelations", family = "cabinRelationsInfo")
	private List<CabinRelationEntityVO> cabinRelations = new ArrayList<CabinRelationEntityVO>();

	/**
	 * 按仓位类型相同的归类展示
	 */
	private List<List<CabinEntityVO>> classifyCabins = new ArrayList<List<CabinEntityVO>>();

	public String getSOURCE_ROWKEY() {
		return SOURCE_ROWKEY;
	}

	/**
	 * 根据返程航班号获得返程航班
	 * 
	 * @param flightNo
	 *            返程航班号
	 * @return
	 */
	public PlaneInfo getReturnPlanInfo(String flightNo) {
		for (PlaneInfo p : returnPlaneInfo) {
			if (StringUtils.isNotBlank(p.getFlightId())
					&& p.getFlightId().equalsIgnoreCase(flightNo))
				return p;
		}
		return null;
	}

	/**
	 * 获得该航班下最低价舱位
	 * 
	 * @param flightNo
	 *            返程航班号
	 * @return
	 */
	public CabinEntityVO getReturnLowestCabinVo(String flightNo) {
		CabinEntityVO lowest = null;
		PlaneInfo planeInfo = getReturnPlanInfo(flightNo);
		if (null != planeInfo) {
			for (CabinEntityVO c : planeInfo.getCabins()) {
				if (c != null) {
					if (lowest == null)
						lowest = c;
					else if (c.getPrice() != null && lowest.getPrice() != null
							&& c.getPrice() < lowest.getPrice())
						lowest = c;
				}

			}
		}
		return lowest == null || lowest.getPrice() == null ? null : lowest;
	}

	/**
	 * 获得去程舱位最低价舱位
	 * 
	 * @return
	 */
	public CabinEntityVO getLowestCabinVo() {
		CabinEntityVO lowest = null;
		for (CabinEntityVO cabin : this.getCabins()) {
			if (cabin != null) {
				if (lowest == null) {
					lowest = cabin;
				} else {
					if (cabin.getPrice() != null && lowest.getPrice() != null
							&& cabin.getPrice() < lowest.getPrice()) {
						lowest = cabin;
					}
				}
			}
		}
		return lowest == null || lowest.getPrice() == null ? null : lowest;
	}

	/**
	 * 当去程舱位和返程舱位同时为null时，找出舱位关系中属于这个返程航班的关系集
	 * 
	 * @param flightNo
	 *            返程航班号
	 * @return
	 */
	public List<CabinRelationEntityVO> getAllReturnCabinRelations(
			String flightNo) {
		List<CabinRelationEntityVO> results = new ArrayList<CabinRelationEntityVO>();
		PlaneInfo planeInfo = getReturnPlanInfo(flightNo);

		if (null != planeInfo
				&& com.fare.taluo.util.StringUtils.checkList(this
						.getCabinRelations())) {
			for (CabinRelationEntityVO cr : this.getCabinRelations()) {
				if (com.fare.taluo.util.StringUtils.checkList(planeInfo
						.getCabins())) {
					for (CabinEntityVO c : planeInfo.getCabins()) {
						if (c.getId().equalsIgnoreCase(cr.getReturnCabinId()))
							results.add(cr);
					}
				}
			}
		}
		return results;
	}

	/**
	 * 获得去程舱位最低价和返程舱位最低价的打包价格
	 * 
	 * @param flightNo
	 * @return
	 */
	public CabinRelationEntityVO getLowestFullPrice(String flightNo) {

		CabinEntityVO cabin = getLowestCabinVo();
		CabinEntityVO returnCabin = getReturnLowestCabinVo(flightNo);
		CabinRelationEntityVO temp = null;

		if (null == returnCabin && null == cabin) {
			List<CabinRelationEntityVO> results = this
					.getAllReturnCabinRelations(flightNo);

			for (CabinRelationEntityVO o : results) {
				if (temp == null) {
					temp = o;
				} else {
					if (o.getFullPrice() != null
							&& temp.getFullPrice() != null
							&& o.getNumberFullPrice() < temp
									.getNumberFullPrice()) {
						temp = o;
					}
				}
			}

			// 若返程舱位为null,或去程舱位为null,则找出返程航班中的直飞航班
		} else if (null == returnCabin || null == cabin) {
			// 找出返程航班
			PlaneInfo planeInfo = getReturnPlanInfo(flightNo);
			if (planeInfo != null) {
				// 找出返程舱位的最低价，根据舱位id找到舱位关系最低价，没有返程舱位则直接返回返程航班的最低价
				if (returnCabin != null) {
					String returnCabinId = returnCabin.getId();
					if (com.fare.taluo.util.StringUtils.checkList(this
							.getCabinRelations())) {
						for (CabinRelationEntityVO relation : this
								.getCabinRelations()) {
							if (relation.getReturnCabinId().equals(
									returnCabinId)) {
								return relation;
							}
						}

					}
				} else {
					CabinRelationEntityVO v = new CabinRelationEntityVO();
					v.setFullPrice(planeInfo.getLowerPrice());
					return v;
				}
			}
		} else if (this.getCabinRelations() != null) {
			for (CabinRelationEntityVO relation : this.getCabinRelations()) {
				if (StringUtils.isNotBlank(relation.getCabinId())
						&& relation.getCabinId()
								.equalsIgnoreCase(cabin.getId())
						&& relation.getReturnCabinId().equalsIgnoreCase(
								returnCabin.getId())) {
					temp = relation;
					break;
				}
			}
		}
		return temp;
	}

	public void setSOURCE_ROWKEY(String sOURCE_ROWKEY) {
		SOURCE_ROWKEY = sOURCE_ROWKEY;
	}

	public String getTimstamp() {
		return timstamp;
	}

	public void setTimstamp(String timstamp) {
		this.timstamp = timstamp;
	}

	private String toCityName = "";

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	/**
	 * 起飞地(源)
	 * 
	 */
	private String fromCity = "";

	private String fromCityName = "";

	private String grabDate;

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	/**
	 * 最低价格
	 */
	private String lowprPrice = "";

	/**
	 * 最高价格
	 */
	private String highPrice = "";

	/**
	 * 抓取渠道
	 */
	private String grabChannel = "";
	/**
	 * 代理商信息
	 */
	private List<AgentInfo> agentInfos;
	private List<AgentEntityVO> agentVO;

	/**
	 * 飞机类型
	 */
	private String type = "";

	/**
	 * 代理商名字
	 */
	private String agentName = "";

	/**
	 * 抓取时间 12312323423时间戳
	 */
	private Date grapDate;

	public String getGrapDateStr() {
		if (StringUtils.isNotBlank(grabDate)) {
			return DateUtils.formatDay(
					DateUtils.StringToDate("yyyyMMddHHmmssSSS", grabDate),
					"yyyy-MM-dd HH:mm");
		} else if(grapDate != null){
			return DateUtils.formatDay(grapDate,"yyyy-MM-dd HH:mm");
		}
		return null;
	}

	public String getGrapDateHStr(){
		if(grapDate != null){
			return DateUtils.formatDay(grapDate,"yyyy-MM-dd HH");
		}
		return null;
	}

	public Date getGrapDate() {
		return grapDate;
	}

	public void setGrapDate(Date grapDate) {
		this.grapDate = grapDate;
	}

	private String grabChannelNum;
	/**
	 * 区域名
	 */
	private String areaName;
	/**
	 * 区域ID
	 * 
	 */
	private String areaId;

	private String areaCode;

	private String grabChannelId;

	private String lowerPrice;

	public List<AgentEntityVO> getAgentVO() {
		return agentVO;
	}

	public void setAgentVO(List<AgentEntityVO> agentVO) {
		this.agentVO = agentVO;
	}

	public String getGrabChannelNum() {
		return grabChannelNum;
	}

	public void setGrabChannelNum(String grabChannelNum) {
		this.grabChannelNum = grabChannelNum;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getAreaId() {
		return areaId;
	}

	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getGrabChannel() {
		return grabChannel;
	}

	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}

	public String getGrabDate() {
		return grabDate;
	}

	public void setGrabDate(String grabDate) {
		this.grabDate = grabDate;
	}

	public List<AgentInfo> getAgentInfos() {
		return agentInfos;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getGrabChannelId() {
		return grabChannelId;
	}

	public void setGrabChannelId(String grabChannelId) {
		this.grabChannelId = grabChannelId;
	}

	public String getLowerPrice() {
		return lowerPrice;
	}

	public void setLowerPrice(String lowerPrice) {
		this.lowerPrice = lowerPrice;
	}

	public void setAgentInfos(List<AgentInfo> agentInfos) {
		this.agentInfos = agentInfos;
	}

	public String getCarrierKey() {
		return carrierKey;
	}

	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}

	public String getCarrierName() {
		return carrierName;
	}

	public void setCarrierName(String carrierName) {
		this.carrierName = carrierName;
	}

	public String getCarrierFullName() {
		return carrierFullName;
	}

	public void setCarrierFullName(String carrierFullName) {
		this.carrierFullName = carrierFullName;
	}

	public String getFlightBatch() {
		return flightBatch;
	}

	public void setFlightBatch(String flightBatch) {
		this.flightBatch = flightBatch;
	}

	public String getFlightBatchDate() {
		return flightBatchDate;
	}

	public void setFlightBatchDate(String flightBatchDate) {
		this.flightBatchDate = flightBatchDate;
	}

	public String getFlightBatchStartTime() {
		return flightBatchStartTime;

		/*
		 * if(flightBatchStartTime.indexOf(":")>0){ return flightBatchStartTime;
		 * }else{ return
		 * flightBatchStartTime.substring(0,2)+":"+flightBatchStartTime
		 * .substring(2,flightBatchStartTime.length()); }
		 */

	}

	public void setFlightBatchStartTime(String flightBatchStartTime) {
		this.flightBatchStartTime = flightBatchStartTime;
	}

	public String getFlightBatchEndTime() {
		return flightBatchEndTime;
	}

	public void setFlightBatchEndTime(String flightBatchEndTime) {
		this.flightBatchEndTime = flightBatchEndTime;
	}

	public String getFlightId() {
		return flightId;
	}

	public void setFlightId(String flightId) {
		this.flightId = flightId;
	}

	public String getLowprPrice() {

		String lower = lowprPrice.indexOf(".") > 0 ? lowprPrice.substring(0,
				lowprPrice.indexOf(".")) : lowprPrice;
		if (StringUtils.isBlank(lower)) {
			if (org.apache.commons.lang3.StringUtils.isNotBlank(lowerPrice)) {
				lower = lowerPrice = lowerPrice.indexOf(".") > 0 ? lowerPrice
						.substring(0, lowerPrice.indexOf(".")) : lowerPrice;
			}
		}
		return lower;
	}

	public void setLowprPrice(String lowprPrice) {
		this.lowerPrice = lowprPrice;
		this.lowprPrice = lowprPrice;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getHighPrice() {
		return highPrice;
	}

	public void setHighPrice(String highPrice) {
		this.highPrice = highPrice;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getAgentName() {
		return agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	public String getRoundCarrierKey() {
		return roundCarrierKey;
	}

	public void setRoundCarrierKey(String roundCarrierKey) {
		this.roundCarrierKey = roundCarrierKey;
	}

	public String getRoundCarrierName() {
		return roundCarrierName;
	}

	public void setRoundCarrierName(String roundCarrierName) {
		this.roundCarrierName = roundCarrierName;
	}

	public String getRoundCarrierFullName() {
		return roundCarrierFullName;
	}

	public void setRoundCarrierFullName(String roundCarrierFullName) {
		this.roundCarrierFullName = roundCarrierFullName;
	}

	public String getRoundFlightBatchDate() {
		return roundFlightBatchDate;
	}

	public void setRoundFlightBatchDate(String roundFlightBatchDate) {
		this.roundFlightBatchDate = roundFlightBatchDate;
	}

	public String getRoundFlightBatchStartTime() {
		return roundFlightBatchStartTime;
	}

	public void setRoundFlightBatchStartTime(String roundFlightBatchStartTime) {
		this.roundFlightBatchStartTime = roundFlightBatchStartTime;
	}

	public String getRoundFlightBatchEndTime() {
		return roundFlightBatchEndTime;
	}

	public void setRoundFlightBatchEndTime(String roundFlightBatchEndTime) {
		this.roundFlightBatchEndTime = roundFlightBatchEndTime;
	}

	/**
	 * 得到起飞时间
	 * <p>
	 * 由flightBatchDate+flightBatchStartTime组合而成
	 * 
	 * @return
	 * @throws ParseException
	 */
	public Date getFlightStartTime() throws ParseException {
		if (StringUtils.isNotEmpty(flightBatchDate)
				&& StringUtils.isNotEmpty(flightBatchStartTime)) {
			String time = flightBatchStartTime.indexOf(":") > -1 ? flightBatchStartTime
					.replace(":", "") : flightBatchStartTime;
			if (time.length() == 3)
				time = "0" + time;
			else if (time.length() == 2)
				time = "00" + time;
			else if (time.length() == 1)
				time = "000" + time;
			return DateUtils.parseDate(flightBatchDate + " " + time,
					"yyyy-MM-dd HHmm");
		}
		return null;
	}
	/**
	 * 获得起飞的小时
	 * @return
	 */
	public Integer getFlightStartHour(){
		if(StringUtils.isNotEmpty(flightBatchStartTime)){
			return Integer.valueOf(flightBatchStartTime.substring(8, 10));
		}
		return 0;
	}
	/**
	 * 获得起飞的小时：分
	 * @return
	 */
	public String getFlightStartHM(){
		Date date;
		try {
			if(StringUtils.isNotEmpty(flightBatchStartTime) && flightBatchStartTime.indexOf("-")<0){
				date =DateUtils.strToDate(flightBatchStartTime, "yyyyMMddHHmmssSSS");
			}else{
				date = getFlightStartTime();
			}
			if(date != null)
				return DateUtils.formatDay(date, "HH:mm");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 获得到达时的小时：分
	 * @return
	 */
	public String getFlightEndHM(){
		Date date;
		try {
			if(StringUtils.isNotEmpty(flightBatchEndTime) && flightBatchEndTime.indexOf("-")<0){
				date =DateUtils.strToDate(flightBatchEndTime, "yyyyMMddHHmmssSSS");
			}else{
				date = getFlightEndTime();
			}
			if(date != null)
				return DateUtils.formatDay(date, "HH:mm");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
	}
	/**
	 * 得到起飞时间
	 * <p>
	 * 由flightBatchDate+flightBatchEndTime组合而成,暂不考虑跨天了
	 * 
	 * @return
	 * @throws ParseException
	 */
	public Date getFlightEndTime() throws ParseException {
		if (StringUtils.isNotEmpty(flightBatchDate)
				&& StringUtils.isNotEmpty(flightBatchEndTime)) {
			String time = flightBatchEndTime.indexOf(":") > -1 ? flightBatchEndTime
					.replace(":", "") : flightBatchEndTime;
			if (time.length() == 3)
				time = "0" + time;
			else if (time.length() == 2)
				time = "00" + time;
			else if (time.length() == 1)
				time = "000" + time;
			return DateUtils.parseDate(flightBatchDate + " " + time,
					"yyyy-MM-dd HHmm");
		}
		return null;
	}

	public List<AgentEntityVO> getAgents() {
		return agents;
	}

	public void setAgents(List<AgentEntityVO> agents) {
		this.agents = agents;
	}

	public List<CabinEntityVO> getCabins() {
		Collections.sort(cabins, new Comparator<CabinEntityVO>(){
			@Override
			public int compare(CabinEntityVO arg0, CabinEntityVO arg1) {
				return arg1.getPrice().compareTo(arg0.getPrice());
			}
		});
		return cabins;
	}

	public void setCabins(List<CabinEntityVO> cabins) {
		this.cabins = cabins;
	}

	public List<PriceEntityVO> getPrices() {
		return prices;
	}

	public void setPrices(List<PriceEntityVO> prices) {
		this.prices = prices;
	}

	public List<TransitchangeEntityVO> getTransit() {
		return transit;
	}

	public void setTransit(List<TransitchangeEntityVO> transit) {
		this.transit = transit;
	}

	public String getCabinInfos() {
		return cabinInfos;
	}

	public void setCabinInfos(String cabinInfos) {
		this.cabinInfos = cabinInfos;
	}

	public Integer getTendency() {
		int rd = RandomUtils.random(100);
		return tendency == null ? (rd<=30 ? 1 : rd>=60 ? -1 : 0) : tendency;
	}

	public void setTendency(Integer tendency) {
		this.tendency = tendency;
	}

	public List<List<CabinEntityVO>> getClassifyCabins() {
		return classifyCabins;
	}

	public void setClassifyCabins(List<List<CabinEntityVO>> classifyCabins) {
		this.classifyCabins = classifyCabins;
	}

	public Integer getFreightType() {
		return freightType;
	}

	public void setFreightType(Integer freightType) {
		this.freightType = freightType;
	}

	public String getRoundTripPrice() {
		return roundTripPrice;
	}

	public void setRoundTripPrice(String roundTripPrice) {
		this.roundTripPrice = roundTripPrice;
	}

	public Double getInterlinePrice() {
		return interlinePrice;
	}

	public void setInterlinePrice(Double interlinePrice) {
		this.interlinePrice = interlinePrice;
	}

	public List<ReturnTripEntityVO> getReturnTrip() {
		return returnTrip;
	}

	public void setReturnTrip(List<ReturnTripEntityVO> returnTrip) {
		this.returnTrip = returnTrip;
	}

	public String getActuallyFlightNo() {
		return actuallyFlightNo;
	}

	public void setActuallyFlightNo(String actuallyFlightNo) {
		this.actuallyFlightNo = actuallyFlightNo;
	}

	public List<StopOverEntityVO> getStopOverlist() {
		return stopOverlist;
	}

	public void setStopOverlist(List<StopOverEntityVO> stopOverlist) {
		this.stopOverlist = stopOverlist;
	}

	public List<PlaneInfo> getReturnPlaneInfo() {
		return returnPlaneInfo;
	}

	public void setReturnPlaneInfo(List<PlaneInfo> returnPlaneInfo) {
		this.returnPlaneInfo = returnPlaneInfo;
	}

	public List<CabinRelationEntityVO> getCabinRelations() {
		return cabinRelations;
	}

	public void setCabinRelations(List<CabinRelationEntityVO> cabinRelations) {
		this.cabinRelations = cabinRelations;
	}

	public String getHbaseKey() {
		return hbaseKey;
	}

	public void setHbaseKey(String hbaseKey) {
		this.hbaseKey = hbaseKey;
	}

}
